package com.hp.appfw.service.cust;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.hp.appfw.data.mapper.mygen.BaseTaskMonitorMapper;
import com.hp.appfw.data.mode.mygen.BaseTaskMonitor;
import com.hp.appfw.data.mode.mygen.BaseTaskMonitorExample;

@Service("taskMonitorService")
public class TaskMonitorService {
	
	@Resource
	private BaseTaskMonitorMapper mapper;

	public void updateStatus()
	{
		BaseTaskMonitorExample example = new BaseTaskMonitorExample();
		
		Date curDate = new Date();
		
		example.createCriteria().andDeleteFlagEqualTo("False");
		List<BaseTaskMonitor> taskList = mapper.selectByExample(example);
		for(BaseTaskMonitor item : taskList)
		{
			long diff = ((curDate.getTime() - item.getUpdateTime().getTime()) / 1000);
			
			if(diff > 3600 && (item.getStatus().equals("Create") || item.getStatus().equals("Run")))
			{
				item.setStatus("Stop");
				item.setUpdateTime(new Date());
				item.setUpdatedBy("system");
				
				mapper.updateByPrimaryKey(item);
			}
			
			if(diff > 3600 * 24 * 3)
			{
				item.setDeleteFlag("True");
				item.setUpdateTime(new Date());
				item.setUpdatedBy("system");
				
				mapper.updateByPrimaryKey(item);
			}
		}
	}
}
